function validate2(form, validatelist)
{
    var ret = true;
    for(k in validatelist)
    {
        for(var i=0; i<form.length;i++)
        {
            if(form[i].name == k)
            {
                for(v1 in validatelist[k])
                {
                    if(!validation(form[i], v1, validatelist[k][v1]))
                    {
                        form[i].style.color='red';
                        form[i].style.borderWidth = '1px';
                        form[i].style.borderStyle = 'dotted';
                        form[i].style.borderColor = 'red';
                        ret = false;
                        break;
                    }
                    else
                    {
                        form[i].style.color = '';
                        form[i].style.borderWidth = '';
                        form[i].style.borderStyle = '';
                        form[i].style.borderColor = '';
                    }
                }
                break;
            }
            
        }
    }
    return ret;
}

function validation(element, type, predefined)
{
    switch(type)
    {
        case "int":
            value = element.value;
            return IsNumeric(value);
            break;
        case "date":
            value = element.value;
            return IsDate(value);
            break;
        case "required":
            value = element.value;
            return(value!='');
            break;
    }
    return true;
}

function validate(form, validatelist)
{
    for (var i = 0; i < validatelist.length; i++)
    {
        var fieldname = validatelist[i][0];
        var pattern = validatelist[i][1];
        var script = validatelist[i][2];
        var referenced = null;
        if(validatelist[i].length > 3)
        {
            referenced = validatelist[i][3];
            if(referenced != null)
            {
                referenced = document.getElementById(referenced);
                if(referenced == null)
                {
                    alert('Отсутствует зависимый объект '+validatelist[i][3]+'.');
                    return false;
                }
            }
        }
        if((referenced == null) || (referenced.style.display == ''))
        {
            var ele = form.elements[fieldname];
            switch(pattern)
            {         
                case "oneormorechecked":
                    var isok = false;
                    for(var j=0;j<ele.length;j++)
                    {
                        if(ele[j].checked)
                        {
                            isok = true;
                            break;
                        }
                    }
                    if(!isok)
                    {
                        eval(script);
                        return false;
                    }
                    break;     
                case "checked":
                    if(!ele.checked){eval(script);return false;}
                    break;
                case "policy_serial":
                    if(!checkmask(PolicySerialMask, ele.value)) {eval(script);return false;}
                    break;
                case "policy_number":
                    if(!checkmask(PolicyNumberMask, ele.value)) {eval(script);return false;}
                    break;
                case "date":
                    pattern = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d";
                    var re = new RegExp(pattern);
                    if (!ele.value.match(re)){eval(script);return false;}
                    break;
                case "required":
                    if (ele.value == ""){eval(script);return false;}
                    break;
                case "selectisnotnull":
                    if(ele.options.value == ''){eval(script);return false;}
                    break;
            }
        }
    }
    return true;
}

function checkmask(mask, value)
{
    var masksymbol = "";
    var symbol = "";
    if(mask == "")
    {
        return true;
    }
    if(mask.length != value.length)
    {
        return false;
    }
    for(var i = 0; i < mask.length; i++)
    {
        masksymbol = mask.substr(i, 1);
        symbol = value.substr(i, 1);
        switch(masksymbol)
        {
            case "9":
                if(!IsNumeric(symbol)){return false;}
                break;
            case "_":
                if(!IsChar(symbol)){return false;}
                break;
            default:
                if(masksymbol!=symbol){return false;}
                break;
        }
    }
    return true;
}

function IsChar(sText)
{
    var ValidChars = "0123456789abcdefghjiklmnopqrstuvwxyzABCDEFGHJIKLMNOPQRSTUVWXYZабвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ";
    var Char;
    for (i = 0; i < sText.length; i++) 
    { 
        Char = sText.charAt(i); 
        if(ValidChars.indexOf(Char) == -1) 
        {
            return false;
        }
    }
    return true;
}

function IsNumeric(sText)
{
    var ValidChars = "0123456789";
    var Char;
    for (i = 0; i < sText.length; i++) 
    { 
        Char = sText.charAt(i); 
        if(ValidChars.indexOf(Char) == -1) 
        {
            return false;
        }
    }
    return true;
}

function IsDate($value)
{
    var arrDate = value.split('/');
    if(arrDate.length!=3){var arrDate = value.split('.');}
    if(arrDate.length!=3){var arrDate = value.split('-');}
    if(arrDate.length!=3){return false;}
    var d = arrDate[0];
    var m = arrDate[1];
    var y = arrDate[2];
    if(!InRange(d,1,31)){return false;} // должно быть больше 0 и меньше 32
    if(!InRange(m,1,12)){return false;} // должо быть больше 0 и меньше 13
    if(!InRange(y,1900,2050)){return false;}
    if(arrDate[1]==2) // если  февраль, то
    {
        if(y % 4 == 0 && y % 100 != 0 || y % 400 == 0) // если висикосный, то
        {
            if(!InRange(d,1,29)){return false;}
        }
        else
        {
            if(!InRange(d,1,28)){return false;}
        }
    }
    //pattern = "(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d";
    //var re = new RegExp(pattern);
    //if (!element.value.match(re)){return false;}
    return true;
}

function InRange($number, $min, $max)
{
    if(!IsNumeric($number)){return false;}
    if($number<$min){return false;}
    if($number>$max){return false;}
    return true;
}